package com.stgl.service;

import com.stgl.dao.HomeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HomeService {
    @Autowired
    private HomeDao homeDao;


    public Map<String,Object>getHomeData()
    {
        Calendar cale = null;
        cale = Calendar.getInstance();
        int nowyear = cale.get(Calendar.YEAR);

        Map<String,Object>smap=new HashMap<String,Object>();
        Map<String,Object>amap=new HashMap<String,Object>();
        amap.put("nowyear",nowyear);
        //性别的统计
        List<Map<String,Object>>sexlist=new ArrayList<>();
        sexlist=homeDao.getStaticSex(amap);

        //年龄的统计
        //查询年龄段
        List<Map<String,Object>>aged=new ArrayList<>();
        aged=homeDao.getAgeDuan(amap);
        for(int i=0;i<aged.size();i++){
            Map<String,Object>maps=aged.get(i);
            List<Map<String,Object>>agelist=new ArrayList<>();
            String age=aged.get(i).get("AGE").toString();
            Map<String,Object>bmap=new HashMap<String,Object>();
            bmap.put("nowyear",nowyear);
            bmap.put("age",age);
            agelist=homeDao.getStaticAge(bmap);

            maps.put("agelist",agelist);
        }


        //学校的统计
        List<Map<String,Object>>camlist=new ArrayList<>();
        camlist=homeDao.getStaticCampus(amap);

        //乡镇的统计
        List<Map<String,Object>>townlist=new ArrayList<>();
        townlist=homeDao.getStaticTown(amap);

        //各学校报名总人数 中签的  未中签的
        List<Map<String,Object>>bmzqlist=new ArrayList<>();
        bmzqlist=homeDao.getStaticBmzq(amap);

        smap.put("sexlist",sexlist);
        smap.put("aged",aged);
        smap.put("camlist",camlist);
        smap.put("townlist",townlist);
        smap.put("bmzqlist",bmzqlist);
        return smap;
    }


    @Cacheable(value="systeminfo")
    public Map<String,Object>getSystemInfo()
    {
        List<Map<String,Object>>list1=new ArrayList<>();
        list1=homeDao.getSystemInfo();
        if(list1.size()>0){
            return list1.get(0);
        }
        return new HashMap<>();
    }


    @CacheEvict(value="systeminfo",allEntries=true)
    public Boolean updateSystemInfo(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=new ArrayList<>();
        list1=homeDao.getSystemInfo();
        int i=0;
        if(list1.size()>0){
            i=homeDao.updateSystemInfo(gmap);
        }else{
            i=homeDao.insertSystemInfo(gmap);
        }
        if(i>0){
            return true;
        }
        return false;
    }

}
